import { _decorator, Component, Label, Node } from 'cc';
import { PopManager } from '../manager/PopManager';
import { EventName, PopView } from '../manager/AppControll';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import EvtManger from '../tools/EvtManger';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('RandomLevelBtn')
export class RandomLevelBtn extends Component {
    private mNdPoint = null;
    private mLbLevel = null;
    private interval = 20;
    private cur = 0;
    protected onLoad(): void {
        this.mNdPoint = this.node.getChildByName("yellowPoint");
        this.mLbLevel = this.node.getChildByName("level").getComponent(Label);
        this.mLbLevel.string = GameData.playerData.randomLevel + "";
        this.node.on(Node.EventType.TOUCH_END, this.OnClick, this);
    }

    protected update(dt: number): void {
        this.cur++;
        if (this.cur % this.interval != 0) return;
        this.mLbLevel.string = GameData.playerData.randomLevel + "";
        let level = GameData.playerData.randomLevel;
        if (level == undefined) {
            GameData.playerData.randomLevel = 1;
            level = 1;
        } else {
            level = GameData.playerData.randomLevel + 1;
        }
        let config = DataHelper.GunRandomSpendConfigByLv(level);
        let isEnough = true;
        if (!config) {
            isEnough = false;
        } else {
            config.upgradeSpend.forEach(element => {
                let num = element[1]
                if (GameData.GetNumByType(element[0]) < num) {
                    isEnough = false;
                };
            });
        }
        this.mNdPoint.active = isEnough;
    }

    OnClick() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.secondGunRandom);
    }
}


